package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.bean.SessionUser;
import com.maixy.studyonlineapi.entity.Manager;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.service.ManagerService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.ActivityView;
import com.maixy.studyonlineapi.view.ManagerView;
import com.maixy.studyonlineapi.view.MomentView;
import com.maixy.studyonlineapi.view.UserView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName ManagerController
 * @Description TODO
 * @Date 2021/3/4 16:43
 * @Author maixy
 */
@RestController
@RequestMapping("/v1/manager")
public class ManagerController extends ApiController {

    @Resource
    public ManagerService managerService;


    /**
     * 运维用户登录
     * @param request -- userName：用户名
     *                -- pwd密码
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    public ApiResponse login(ApiRequest request) {
        String userName = request.getDataParamAsString("userName");
        String pwd = request.getDataParamAsString("pwd");

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(pwd)) {
            throw new MissParamException();
        }

        Manager manager = managerService.findManagerByUserNameAndPwd(userName, pwd);
        if (null == manager || manager.getHasDelete()) {
            throw new TipException("用户名或密码不正确，请检查一下");
        }
        SessionUser sessionUser = managerService.buildManagerInfo(manager);
        return ApiResponse.ok()
                .addValueToData("user", sessionUser)
                .addValueToData("teamId",String.valueOf(1));
    }

    /**
     * 获得个人的基本信息
     * @param request -- userName：用户名
     *                -- pwd密码
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse getManagerBasisInfo(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());

        return ApiResponse.ok()
                .addValueToData("managerBasisInfoView", managerService.getManagerBasisInfo(userId));
    }


    /**
     * 我创建的活动列表
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse getActivityList(ApiRequest request) {
        String activitySearch = request.getDataParamAsString("activitySearch", "");
        Integer activityType = request.getDataParamAsInt("activityType");
        Integer activityStatus = request.getDataParamAsInt("activityStatus");
        Page page = Page.validation(request.getPage());
        PageData<ActivityView> pageData = managerService.getActivityList(activitySearch, activityType, activityStatus, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }


    /**
     * 违规设置
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse setViolation(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        Long managerId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (null == activityId) {
            throw new TipException("参数缺失");
        }
        managerService.setViolation(activityId,1,managerId);
        return ApiResponse.ok();
    }


    @Privilege
    public ApiResponse getUserList(ApiRequest request) {
        String nickNameSearch = request.getDataParamAsString("nickNameSearch", "");
        Page page = Page.validation(request.getPage());
        PageData<UserView> pageData = managerService.getUserList(nickNameSearch, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }

    @Privilege
    public ApiResponse getManagerList(ApiRequest request) {
        String userNameSearch = request.getDataParamAsString("userNameSearch", "");
        Page page = Page.validation(request.getPage());
        PageData<ManagerView> pageData = managerService.getManagerList(userNameSearch, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }

    @Privilege
    public ApiResponse addManager(ApiRequest request) {
        String userName = request.getDataParamAsString("userName", "");
        String pwd = request.getDataParamAsString("pwd", "");
        Long managerId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(pwd)) {
            throw new TipException("参数缺失");
        }
        managerService.addManager(userName, pwd, managerId);
        return ApiResponse.ok();
    }

    @Privilege
    public ApiResponse updateManager(ApiRequest request) {
        Long currentManagerId = request.getDataParamAsLong("currentManagerId");
        Long managerId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (null == currentManagerId) {
            throw new TipException("参数缺失");
        }
        managerService.updateManager(currentManagerId, managerId);
        return ApiResponse.ok();
    }

    @Privilege
    public ApiResponse getMomentList(ApiRequest request) {
        String momentSearch = request.getDataParamAsString("momentSearch", "");
        Integer momentStatus = request.getDataParamAsInt("momentStatus");
        Page page = Page.validation(request.getPage());
        PageData<MomentView> pageData = managerService.getMomentList(momentSearch, momentStatus, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }


    /**
     * 打卡违规设置
     * @param request --
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse setMomentViolation(ApiRequest request) {
        Long momentId = request.getDataParamAsLong("momentId");
        Long managerId = DataTypeUtil.getLong(request.getUser().getUserId());

        if (null == momentId) {
            throw new TipException("参数缺失");
        }
        managerService.setViolation(momentId, 2, managerId);
        return ApiResponse.ok();
    }

}
